//bench.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({
        data: {
                inputName: '',
                selGoodsList: []
        },

        onLoad: function (options) {
                this.setData({
                        warehouseId: options.warehouseId,
                        optaction: options.optaction,
                        uid: options.uid,
                        selGoodsList: options.selGoodsList ? JSON.parse(options.selGoodsList) : [],
                        scrollHeight: app.getScrollHeight(155)
                });
                util.print('selGoodsList', this.data.selGoodsList);
                let barTitle = options.warehouseName;
                if (options.optaction == 1) { //出库

                } else if (options.optaction == 2) { //入库
                        barTitle = '物品库';
                        if (options.uid) {
                                barTitle = options.uname + barTitle;
                        }

                }else if(options.optaction == 3){
                        barTitle = '库存流水';
                }
                wx.setNavigationBarTitle({
                        title: barTitle
                })
                this.getGoodsCategoryList();
        },
        onReady: function () {
                // Do something when page ready.
        },
        onShow: function () {
                // Do something when page show.
        },
        onHide: function () {
                // Do something when page hide.
        },
        onUnload: function () {
                // Do something when page close.
        },

        inputName: function (e) {
                let inputName = e.detail.value
                this.setData({
                        inputName: inputName
                })
        },
        searchGoods: function () {
                let that = this;
                that.getWarehouseGoods();
        },
        toggleTab: function (e) {
                let id = e.currentTarget.dataset.id;
                if (id == this.data.tabNum) {
                        return;
                }
                this.setData({
                        tabNum: id
                });
                this.getWarehouseGoods();
        },
        getWarehouseGoods: function () {
                let that = this
                let params = {};
                let tabNum = that.data.tabNum;
                let inputName = that.data.inputName;
                if (tabNum) {
                        params.categoryId = tabNum;
                }
                if (inputName) {
                        params.goodsName = inputName;
                        params.name = inputName;
                }
                if (that.data.warehouseId) {
                        params.warehouseId = that.data.warehouseId;
                }
                if (that.data.optaction == 1) { //出库
                        lbbService.getWarehouseGoods(params, function (res) {
                                let goodsList = that.selGoodsAddNumInit(res.data);
                                that.setData({
                                        warehouseGoods: goodsList
                                })
                        })
                } else if (that.data.optaction == 2) { //入库
                        if (that.data.uid) {
                                that.setData({
                                        isGoodsNum: true
                                })
                                params.uid = that.data.uid;
                                lbbService.getOwnerGoods(params, function (res) {
                                        let goodsList = that.selGoodsAddNumInit(res.data);
                                        that.setData({
                                                warehouseGoods: goodsList
                                        })
                                })
                        } else {
                                that.setData({
                                        isGoodsNum: false
                                })
                                lbbService.getGoodsList(params, function (res) {
                                        let goodsList = that.selGoodsAddNumInit(res.data);
                                        that.setData({
                                                warehouseGoods: goodsList
                                        })
                                })
                        }

                } else if (that.data.optaction == 3) {//查看库存流水
                        that.setData({
                                isGoodsNum: false
                        })
                        lbbService.getGoodsList(params, function (res) {
                                that.setData({
                                        warehouseGoods: res.data
                                })
                        })
                }


        },
        selGoodsAddNumInit(data) {
                let list = this.data.selGoodsList
                util.print('this.data.selGoodsList', this.data.selGoodsList, data);
                for (var j = 0; j < data.length; j++) {
                        for (var i = 0; i < list.length; i++) {
                                let goodsId = this.data.optaction == 2 && !this.data.uid ? data[j].goodsStat.id : data[j].goodsId;
                                if (goodsId == list[i].goodsId) { // todo
                                        data[j].addNum = list[i].addNum;
                                }
                        }
                }
                util.print('data', data);
                return data;
        },

        getGoodsCategoryList: function () {
                let that = this;
                lbbService.getGoodsCategoryList({}, function (res) {
                        //let tabNum = res.data[0].id;
                        res.data.unshift({
                                "id": 0,
                                "name": "全部"
                        });
                        that.setData({
                                categories: res.data,
                                tabNum: '',
                        });
                        that.getWarehouseGoods();
                })
        },


        selectGoods: function (e) {
                let that = this;
                console.log("选择物品的计数器的数量事件", e);

                let pages = getCurrentPages()
                let prevPage = pages[pages.length - 2]
                let item = e.currentTarget.dataset.item;
                if(that.data.optaction == 3){
                        prevPage.setData({
                                selectGoods: {goodsName:item.name,goodsStatId:item.goodsStat.id},
                                addGoodsFlag: true,
                        })
                        wx.navigateBack({
                                delta:1
                        })
                        return;
                }
                let selCount = e.detail.value;
                item.addNum = selCount;
                let selectGoods = {};
                if (that.data.optaction == 1 || (that.data.optaction == 2 && that.data.uid > 0)) { //出库
                        selectGoods.goodsId = item.goodsId;
                        selectGoods.goodsName = item.goods.name;
                        selectGoods.goodsUnit = item.goods.goodsUnit;
                        selectGoods.goodsStatId = item.goodsId;
                        selectGoods.goodsNum = item.goodsNum;
                        selectGoods.addNum = item.addNum;
                } else if (that.data.optaction == 2) { //入库
                        selectGoods.goodsId = item.goodsStat.id;
                        selectGoods.goodsName = item.name;
                        selectGoods.goodsUnit = item.goodsUnit;
                        selectGoods.goodsStatId = item.goodsStat.id;
                        selectGoods.goodsNum = item.goodsNum;
                        selectGoods.addNum = item.addNum;
                }
                //将选择的物品加入购物车      
                let list = this.data.selGoodsList;
                let isExists = false;
                for (var i = 0; i < list.length; i++) {
                        let item = list[i];
                        util.print('selectGoods',item.goodsId,selectGoods.goodsId);
                        if (item.goodsId == selectGoods.goodsId) {
                                if (selCount == 0) {
                                        list[i] = null;
                                } else {
                                        list[i].goodsNum = selectGoods.goodsNum;
                                        list[i].addNum = selCount;
                                }
                                isExists = true;
                        }
                }
                if (!isExists) {
                        list.push(selectGoods);
                }

                let selList = [];
                for (var i = 0; i < list.length; i++) {
                        if (list[i] != null) {
                                selList.push(list[i]);
                        }
                }

                prevPage.setData({
                        selectGoods: selectGoods,
                        addGoodsFlag: true,
                        selGoodsList: selList
                })

        },

        selOkSubmit: function (e) {
                wx.navigateBack({
                        delta: 1
                })
        }
})